#! /bin/bash

source ./scheme.sh

if  [ $# -ne 1 ]; then
    echo -e "${BRed} USAGE: ./install.sh local|dev|pre|online|ip ${RCol}"
    exit 1
fi

destination="${1}"
case ${destination} in
    "local")
        dbconfig="db_local.php" 
		index="index.php"
		sspconfig="ssp.php"
		package="package.php"
        params="params.php"
        redis="redis.php"
        accuenconfig="accuen.php"
        isonline=0
        ;;
    "dev")
        dbconfig="db_dev.php" 
		index="index_dev.php"
		sspconfig="ssp_dev.php"
		package="package.php" 
        params="params_dev.php"
        redis="redis_dev.php"
        accuenconfig="accuen_dev.php"
        isonline=0
        ;;
    "pre")
        dbconfig="db_pre.php"  
		index="index_pre.php"
		sspconfig="ssp_pre.php"
		package="package_pre.php"
        params="params_pre.php"
        redis="redis_pre.php"
        accuenconfig="accuen_pre.php"
        isonline=1
        ;;
    "online")
        dbconfig="db_online.php"  
		index="index_online.php"
		sspconfig="ssp_online.php"
		package="package_online.php"
        params="params_online.php"
        redis="redis_online.php"
        accuenconfig="accuen_online.php"
        isonline=1
        ;;
    *)
        dbconfig="db_local.php" 
		index="index.php"
		sspconfig="ssp.php"
		package="package.php"
        params="params.php" 
        redis="redis.php"
        accuenconfig="accuen.php"
        isonline=0
	;;
esac

# install log file
log="web/install.log";
declare -a errors=()
declare -a checks=()

failed="${BRed}Agent AdServer ui update failed!!!${RCol}";
success="${BGre}Congratulations! Agent AdServer UI ${tagdate} deployed successfully!!${RCol}";

php="/opt/lampp/bin/php";

#check environment
#1 php version should gt 5.4.13
declare -i php_version=`${php} -r 'echo version_compare(PHP_VERSION, "5.4.13", ">=");'`
if [ ${php_version} -eq 1 ]; then
    check="Checking PHP version (5.4.13 and above required) ------ ${Gre}OK!${RCol}"
else
    check="Current PHP version is lower than 5.4.13 ------ ${Red}NG!${RCol}";
    errors=("${errors[@]}" "${check}");
fi
echo -e ${check};
checks=("${checks[@]}" "${check}");

#2 checking modules pdo_mysql, curl, json
declare -a required_mods=('curl' 'json' 'mysql' 'mysqli' 'pdo' 'pdo_mysql' 'gd' 'gettext' 'mbstring' 'SimpleXML' 'zip' 'fileinfo');
for mod in ${required_mods[*]}
do
    result=`${php} -m | grep ${mod}`
    if [ $? -eq 0 ]; then
        check="Checking required module: ${mod} ------ ${Gre}OK!${RCol}";
    else
        check="Checking required module: ${mod} ------ ${Red}NG!${RCol}";
        errors=("${errors[@]}" "${check}");
    fi
    checks=("${checks[@]}" "${check}");
    echo -e ${check};
done

# if [ ${isonline} -eq 1 ]; then
#    `sudo cp -f online_pack/online.php index.php`
# fi
if [ ${isonline} -eq 1 ]; then
    rm -rf web/src
fi

`sudo cp -f config/db/${dbconfig} config/db/db.php`
`sudo cp -f config/ssp/${sspconfig} config/ssp/ssp.php`
`sudo cp -f config/package/${package} config/package/package.php`
`sudo cp -f config/params/${params} config/params/params.php`
`sudo cp -f config/redis/${redis} config/redis/redis.php`
`sudo cp -f config/accuen/${accuenconfig} config/accuen/accuen.php`
`sudo cp -f web/${index} web/index.php`
`sudo cp -Rf web/admath/d/* /home/www/vamdsp/vamaker_dsp_uploads/d/`

# check db config
configdir="config/";
config="web.php";
dbok="OK"

${php} ./yii migrate/new 

if [[ $? -eq 0 ]]; then
    check="Checking db connection ------ ${Gre}OK!${RCol}";
else
    check="Checking dbconnection ------ ${Red}NG!${RCol}";
	dbok="NG"
    errors=("${errors[@]}" "${check}");
    errors=("${errors[@]}" "${dbconn}");
fi
echo -e ${check};
checks=("${checks[@]}" "${check}");


# migration up
# ${php} ./yii migrate up<<< $'yes\n' >> /dev/null
${php} ./yii migrate up<<< $'yes\n'

if [[ {$dbok}="OK" ]]; then
    check="Migrate database ------ ${Gre}OK!${RCol}";
else
    check="Migrate database ------ ${Red}NG!${RCol}";
    errors=("${errors[@]}" "${check}");
fi
checks=("${checks[@]}" "${check}");
echo -e ${check};

# modify product config
dir_app="./";
dir_upload="./web/admath";
dir_runtime="${dir_app}runtime";
dir_assets="${dir_app}/web/assets";
mod=777;

# change ui to daemon group
pwd=`pwd`
`sudo chgrp daemon ${pwd} -Rf`

if [[ $? -eq 0 ]]
then
    check="Add current dir to daemon group ------ ${Gre}OK!${RCol}";
else 
    check="Add current dir to daemon group ------ ${Red}NG!${RCol}";
    errors=("${errors[@]}" "${check}");
    result=1;
fi

#set mod func
function set_mod {
    result=0; 
	sudo chmod -R $1 $2
    if [[ $? -eq 0 ]]
    then
        check="Set $2 mod as $1 ------ ${Gre}OK!${RCol}";
    else 
        check="Set $2 mod as $1 ------ ${Red}NG!${RCol}";
        errors=("${errors[@]}" "${check}");
        result=1;
    fi
    echo -e ${check};
    checks=("${checks[@]}" "${check}");
    return ${result};
}

#clean Runtime/*
`sudo rm ${dir_runtime}/* -Rf`

if [[ $? -eq 0 ]]; then
    check="Cleanning Runtime directory ------ ${Gre}OK!${RCol}";
else 
    check="Cleanning Runtime directory ------ ${Red}NG!${RCol}";
    errors=("${errors[@]}" "${check}");
    result=1;
fi
echo -e ${check};
checks=("${checks[@]}" "${check}");

#set runtime mod
if [ ! -d ${dir_runtime} ]; then
    `mkdir ${dir_runtime}`
fi
set_mod ${mod} ${dir_runtime}

#set upload mod
if [ ! -d ${dir_upload} ]; then
    mkdir ${dir_upload}
fi
set_mod ${mod} ${dir_upload}

#set assets mod
if [ ! -d ${dir_assets} ]; then
    mkdir ${dir_assets}
fi
set_mod ${mod} ${dir_assets}

#set upload mod
# if [ ! -d ${dir_upload_exchange} ]; then
#     mkdir ${dir_upload_exchange}
# fi
# set_mod ${mod} ${dir_upload_exchange}


# check install
if [[ ${#errors} -eq 0 ]]; then
    echo -e ${success};
    checks=("${checks[@]}" "${success}");
    retcode=0;
else
    echo -e ${failed};
    checks=("${checks[@]}" "${failed}");
    retcode=1;
fi

echo `date` > ${log}
echo ------------------------------------------------------------------------ >> ${log}
for ret in "${checks[@]}"
do
    echo -e "${ret}" | tee >> ${log}
done
echo ------------------------------------------------------------------------ >> ${log}

# return code
exit ${retcode}
